﻿@inherits PmComponentBase

<SSimpleModal ValueChanged="AppModalValueChanged" Value="_appFormModel.Visible" Title="@(_appFormModel.HasValue ? T("Edit App"):T("Add App"))" Width="770">
    <MForm @ref="_form" Class="full-height" Context="formContext" EnableValidation Model="_appFormModel.Data">
        <div class="d-flex flex-column" style="width:100%; height:100%; overflow: hidden;">
            <div class="full-height mb-2 pr-5" style="overflow: auto;">
                <STextField Class="mt-1" Outlined Label="@T("Name")" @bind-Value="_appFormModel.Data.Name"></STextField>
                <SSelect Value="_appFormModel.Data.Type"
                         ValueChanged="AppTypeValueChanged"
                         ValueExpression="()=>_appFormModel.Data.Type"
                         HideDetails="true"
                         Disabled="@(_appFormModel.HasValue)"
                         Class="mt-9"
                         Items="EnumUtil.GetEnumList<AppTypes>()"
                         ItemText="r=>r.Name"
                         ItemValue="r=>r.Value"
                         TItem="EnumObject<AppTypes>"
                         TItemValue="AppTypes"
                         TValue="AppTypes"
                         Outlined
                         MenuProps="p=>{p.OffsetY = true;}"
                         Label="@T("Type")">
                </SSelect>
                @if (_appFormModel.Data.Type == AppTypes.Service)
                {
                    <SSelect Class="mt-9"
                             @bind-Value="_appFormModel.Data.ServiceType"
                             HideDetails="true"
                             Items="EnumUtil.GetEnumList<ServiceTypes>()"
                             ItemText="r=>r.Name"
                             ItemValue="r=>r.Value"
                             Outlined
                             MenuProps="p=>{p.OffsetY = true;}"
                             Label="@T("Service type")">
                    </SSelect>
                }
                <STextField Class="mt-9" Disabled="@(_appFormModel.HasValue)" Outlined Label="ID" Placeholder="@(_appFormModel.Data.ServiceType==ServiceTypes.Dapr ? T("DAPRAPPNameSuggest") : "")" @bind-Value="_appFormModel.Data.Identity"></STextField>
                <SAutoComplete Class="mt-9"
                               Value="_appFormModel.Data.EnvironmentClusterInfos.Select(e=>e.EnvironmentClusterId).ToList()"
                               Items="_projectEnvClusters"
                               ItemText="r=>r.EnvironmentClusterName"
                               ItemValue="r=> r.Id"
                               Outlined
                               Label="@T("Environment/Cluster")"
                               Multiple
                               TItem="EnvironmentClusterDto"
                               TItemValue="int"
                               TValue="List<int>"
                               ValueChanged="OnEnvironmentClusterSelectedItemUpdate">
                    <SelectionContent Context="context">
                        <EnvClusterChip EnvironmentName="@context.Item.EnvironmentName"
                                        EnvironmentColor="@context.Item.EnvironmentColor"
                                        ClusterName="@context.Item.ClusterName"
                                        Close
                                        OnCloseClick="()=>_appFormModel.Data.EnvironmentClusterInfos.RemoveAll(ec=>ec.EnvironmentClusterId == context.Item.Id)" />
                    </SelectionContent>
                    <ItemContent>
                        <MListItemContent>
                            <div class="d-flex align-center">
                                <div style="@($"color: {context.Item.EnvironmentColor} !important")">@context.Item.EnvironmentName</div><div class="ml-1">@context.Item.ClusterName</div>
                            </div>
                        </MListItemContent>
                    </ItemContent>
                </SAutoComplete>
                @{
                    foreach (var item in _appFormModel.Data.EnvironmentClusterInfos)
                    {
                        var environmentCluster = _projectEnvClusters.Find(p => p.Id == item.EnvironmentClusterId) ?? new();
                        <STextField @key="@environmentCluster.EnvironmentClusterName" Class="mt-9" Outlined Label="URL" @bind-Value="item.Url">
                            <PrependInnerContent>
                                <div class="mt-2 d-flex">
                                    <div style="@($"color: {environmentCluster.EnvironmentColor} !important")" class="text-overflow">@environmentCluster.EnvironmentName</div>
                                    <div style="color:#05CD99" class="text-overflow ml-1">@environmentCluster.ClusterName</div>
                                    <div class="mx-1 regular3--text">|</div>
                                </div>
                            </PrependInnerContent>
                        </STextField>
                    }

                    if (_appFormModel.Data.Type == AppTypes.Service)
                    {
                        foreach (var item in _appFormModel.Data.EnvironmentClusterInfos)
                        {
                            var environmentCluster = _projectEnvClusters.Find(p => p.Id == item.EnvironmentClusterId) ?? new();
                            <STextField @key="@("service:"+ environmentCluster.EnvironmentClusterName)" Class="mt-9" Outlined Label="SwaggerURL" @bind-Value="item.SwaggerUrl">
                                <PrependInnerContent>
                                    <div class="mt-2 d-flex">
                                        <div style="@($"color: {environmentCluster.EnvironmentColor} !important")" class="text-overflow">@environmentCluster.EnvironmentName</div>
                                        <div style="color:#05CD99" class="text-overflow ml-1">@environmentCluster.ClusterName</div>
                                        <div class="mx-1 regular3--text">|</div>
                                    </div>
                                </PrependInnerContent>
                            </STextField>
                        }
                    }
                }
                <StaffSelect Class="mt-9" Label="@T("ResponsibilityUsers")" @bind-Value="_appFormModel.Data.ResponsibilityUsers"></StaffSelect>
                <MTextarea NoResize Class="mt-9" Height="121" Outlined Label="@T("Description")" @bind-Value="_appFormModel.Data.Description"></MTextarea>
                @if (_appFormModel.HasValue)
                {
                    <MRow Class="mt-2">
                        <MCol Cols=6 Class="light-blue-color">@T("Creator"): @AppDetail.CreatorName</MCol>
                        <MCol Class="d-flex justify-end translucency-blue-color" Cols=6>
                            @T("CreationTime"):&nbsp;
                            <DateTimeZone Value="AppDetail.CreationTime" />
                        </MCol>
                    </MRow>
                    <MRow Class="mt-6">
                        <MCol Cols=6 Class="light-blue-color">@T("Modifier"): @AppDetail.ModifierName</MCol>
                        <MCol Class="d-flex justify-end translucency-blue-color" Cols=6>
                            @T("ModificationTime"):&nbsp;
                            <DateTimeZone Value="AppDetail.ModificationTime" />
                        </MCol>
                    </MRow>
                }
            </div>
            <div class="d-flex align-end mt-9" style="width:100%;">
                @if (_appFormModel.HasValue)
                {
                    <SButton Icon Color="#FF5252" Depressed Class="delete-btn" OnClick="RemoveAppAsync">
                        <SIcon Color="red">mdi-delete</SIcon>
                    </SButton>
                }
                <MSpacer></MSpacer>
                <SButton Height="56" Class="rounded-pill" Color="primary" Width="140" OnClick="()=>SubmitAppAsync(formContext)">@(_appFormModel.HasValue ? T("Save") : T("Submit"))</SButton>
            </div>
        </div>
    </MForm>
</SSimpleModal>
